#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/voxel_grid.h>  // 包含体素网格化的头文件
#include <pcl/visualization/pcl_visualizer.h>
 
int main(int argc, char** argv)
{
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>);
    if (pcl::io::loadPCDFile<pcl::PointXYZ>("/root/PCL/Chinadragon.pcd", *cloud) == -1)
    {
        PCL_ERROR("Error loading cloud.pcd file!\n");
        return (-1);
    }
    pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_filtered(new pcl::PointCloud<pcl::PointXYZ>);
    pcl::VoxelGrid<pcl::PointXYZ> voxel_grid;
    voxel_grid.setInputCloud(cloud);
    voxel_grid.setLeafSize(0.01f, 0.01f, 0.01f);  // 设置体素大小为0.01m
    voxel_grid.filter(*cloud_filtered);  // 体素网格化
    pcl::visualization::PCLVisualizer::Ptr viewer(new pcl::visualization::PCLVisualizer("3D Viewer"));
    viewer->setBackgroundColor(0, 0, 0);  // 设置背景颜色为黑色
    viewer->addPointCloud<pcl::PointXYZ>(cloud_filtered, "voxel grid cloud");  // 添加体素网格化后的点云
    viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 2, "voxel grid cloud");  // 设置点的大小为2
    while (!viewer->wasStopped())
    {
            viewer->spinOnce(100);

            }


}